<template>
	<view class="order-list">
		<!-- 顶部导航 -->
		<view class="nav-bar">
			<view v-for="(item, index) in navItems" :key="index+''" :class="['nav-item', { active: currentTab === item.lab }]"
				@tap="changeTab(item.lab)">
				{{ item.name }}
			</view>
		</view>

		<!-- 订单列表 -->
		<scroll-view scroll-y class="order-scroll">
			<view v-if="orderList.length === 0" class="empty-list">
				<!-- <image src="/static/empty-box.png" mode="aspectFit" class="empty-icon"></image> -->
				<text>暂无订单</text>
			</view>
			<view v-else class="order-item" v-for="(order, index) in orderList" :key="index+''" @click="toOrderDetail(order)">
				<view class="order-header">
					<view class="">
						<text class="orderstatus">{{order.deliveryMethod}}</text>
						<text class="order-number">订单号: {{ order.orderNumber }}</text>
					</view>
					<text :class="['order-status', order.statusClass]">{{ stateInfo(order.status) }}</text>
				</view>
				<view class="order-dateparent">
					<text class="order-date">下单时间: {{ order.orderTime }}</text>
				</view>
				<view class="order-content">
					<view class="order-content-left" >
						<image v-for="(item, index) in order.items" :key="index" :src="getImgUrl+item.goodsImg" mode="aspectFill" class="product-image"></image>
						
					</view>
					<view class="order-content-right">
						<view class="order-amount"> ¥{{order.totalPrice }}</view>
						<view class="num">共{{order.items.length}}件</view>
					</view>
				</view>
				<view class="order-footer">
					<view class="">
						<button class="action-button" :class="order.actionClass">{{ order.actionText }}</button>
					</view>


				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
	import {
		computed,
		onMounted,
		ref,
		unref,toRaw 
	} from "vue"
	import {
		onLoad,
		onShow,
		onUnload
	} from '@dcloudio/uni-app';
	import {
		userInfo
	} from '../../stores/userInfo'


	let UserInfo = userInfo()
	let navItems = ref([{
		name: '全部',
		lab: 0
	}, {
		name: '待接单',
		lab: 7
	}, {
		name: '待配送',
		lab: 2
	}, {
		name: '配送中',
		lab: 4
	},{
		name: '待自提',
		lab: 3
	}
	,{
		name: '已完成',
		lab: 5
	}])
	let currentTab = ref(0)
	// let orderList = ref([])
	
	// 订单数据
	let orderData = ref([])

	let changeTab = (index) => {
		currentTab.value = index;
		// getOrderList();
	}
	
	let stateInfo = (state)=>{
		let orderStatusName = ""
		 navItems.value.map(item=>{
			// console.log(item,state)
			if(item.lab==state){
				orderStatusName =  item.name
			}
		})
		
		return orderStatusName
	}
	// let getOrderList = () => {
	// 	// 模拟数据，实际应用中需要替换为真实的 API 调用
	// 	const mockData = [{
	// 			orderNumber: 'OR001',
	// 			status: '待支付',
	// 			statusClass: 'status-pending',
	// 			amount: 99.99,
	// 			productName: '时尚休闲鞋',
	// 			productImage: '/static/detail/clothes.png',
	// 			orderDate: '2023-04-15 14:30',
	// 			actionText: '立即支付',
	// 			actionClass: 'pay-button'
	// 		},
	// 		{
	// 			orderNumber: 'OR002',
	// 			status: '待发货',
	// 			statusClass: 'status-processing',
	// 			amount: 199.99,
	// 			productName: '智能手表',
	// 			productImage: '/static/detail/clothes.png',
	// 			orderDate: '2023-04-14 09:15',
	// 			actionText: '催促发货',
	// 			actionClass: 'urge-button'
	// 		},
	// 		{
	// 			orderNumber: 'OR003',
	// 			status: '已完成',
	// 			statusClass: 'status-completed',
	// 			amount: 299.99,
	// 			productName: '无线耳机',
	// 			productImage: '/static/detail/clothes.png',
	// 			orderDate: '2023-04-10 18:45',
	// 			actionText: '评价',
	// 			actionClass: 'review-button'
	// 		},
	// 	];

	// 	orderList.value = mockData.filter(order => {
	// 		if (currentTab.value === 0) return true;
	// 		return order.status === navItems.value[currentTab.value];
	// 	});
	// }


	//这是点击订单列表的单个商品信息
	let toOrderDetail = (item)=>{
		console.log(item,"这是点击订单列表的单个商品信息")
		uni.navigateTo({
			url: '/pages/take-foods/take-foods',
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('orderDetailData', {
					data: item
				})
			}
		})
	}

	// 获取图片地址
	let getImgUrl = computed(() => {
		return UserInfo.getimgUrl()
	})

	let orderList = computed(() => {

		// console.log(toRaw (UserInfo?.userOrderData1),"UserInfo.userOrderData1")
		return orderData.value.filter(order => {
			if (currentTab.value === 0) return true;
			return order.status === currentTab.value;
		});
	})

	// let toOrderDetail = () => {
	// 	// this.uni.nab
	// 	uni.navigateTo({

	// 	})
	// }

	onMounted(() => {

		// getOrderList();

	})

	onShow( async() => {
		if (UserInfo.userinfoData?.wxLoginCode) {
		  let res = await	UserInfo.getOrdersByWxuserId({
				wxLoginCode: UserInfo.userinfoData.wxLoginCode
			})
		orderData.value = res
		}
	})
</script>

<style lang="scss" scoped>
	.order-list {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #f5f5f5;
	}

	.nav-bar {
		display: flex;
		justify-content: space-around;
		padding: 20rpx 0;
		background-color: #ffffff;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.nav-item {
		font-size: 28rpx;
		color: #666;
		padding: 10rpx 20rpx;
		border-radius: 30rpx;
		transition: all 0.3s ease;

		&.active {
			color: #ffffff;
			background-color: #007aff;
			font-weight: bold;
		}
	}

	.order-scroll {
		width: 95%;
		padding: 20rpx;
	}

	.order-item {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
	}

	.order-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.orderstatus {
		font-size: 12px;
		color: red;
		background-color: #ffeaeb;
		padding: 4px;
		border-radius: 2px;
		margin-right: 20px;
	}

	.order-number {
		font-size: 26rpx;
		color: #666;
	}

	.order-status {
		font-size: 24rpx;
		padding: 6rpx 16rpx;
		border-radius: 20rpx;

		&.status-pending {
			background-color: #ffefd5;
			color: #ffa500;
		}

		&.status-processing {
			background-color: #e6f7ff;
			color: #1890ff;
		}

		&.status-completed {
			background-color: #f6ffed;
			color: #52c41a;
		}
	}

	.order-content {
		display: flex;
		margin-bottom: 20rpx;
		justify-content: space-between;
		align-items: center;

		.order-content-left {
			display: flex;
			width: 80%;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			display: block
		}

		.order-content-right {
			width: 20%;
			display: flex;
			flex-direction: column;
			align-items: center;

		}
	}

	.product-image {
		width: 160rpx;
		height: 160rpx;
		border-radius: 10rpx;
		margin-right: 20rpx;
	}

	.order-details {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.product-name {
		font-size: 28rpx;
		font-weight: bold;
		color: #333;
	}


	.order-dateparent {
		margin-bottom: 10px;

		.order-date {
			font-size: 14px;
			color: black;

		}
	}

	.order-footer {
		display: flex;
		flex-direction: row-reverse;
		// justify-content: space-between;
		align-items: none;
		// width: 100%;
	}

	.order-amount {
		font-size: 32rpx;
		font-weight: bold;
		color: #000;

	}

	.num {
		font-size: 14px;
		color: #686868;
		margin-top: 2px;
	}

	.action-button {
		font-size: 24rpx;
		padding: 10rpx 30rpx;
		border-radius: 30rpx;
		// border: none;
		width: 100px;
		// flex-grow: 4;

		&.pay-button {
			background-color: #ff6b6b;
			color: #ffffff;
		}

		&.urge-button {
			background-color: #ffd166;
			color: #ffffff;
		}

		&.review-button {
			background-color: #4ecdc4;
			color: #ffffff;
		}
	}

	.empty-list {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100%;
		color: #999;

		.empty-icon {
			width: 200rpx;
			height: 200rpx;
			margin-bottom: 20rpx;
		}
	}
</style>